import { Request, Response } from 'express';
import { TeacherService } from '../services/teacher.service';

const teacherService = new TeacherService();

export class TeacherController {
    async getTeachers(req: Request, res: Response) {
        try {
            const teachers = await teacherService.getTeachers();
            res.json({ data: teachers });
        } catch (error) {
            res.status(500).json({ message: '服务器错误' });
        }
    }

    async getTeacherById(req: Request, res: Response) {
        try {
            const teacherId = parseInt(req.params.id);
            const teacher = await teacherService.getTeacherById(teacherId);
            
            if (!teacher) {
                return res.status(404).json({ message: '讲师不存在' });
            }
            
            res.json({ data: teacher });
        } catch (error) {
            res.status(500).json({ message: '服务器错误' });
        }
    }

    async getTeacherCourses(req: Request, res: Response) {
        try {
            const teacherId = parseInt(req.params.id);
            const teacher = await teacherService.getTeacherById(teacherId);
            
            if (!teacher) {
                return res.status(404).json({ message: '讲师不存在' });
            }

            const courses = await teacherService.getTeacherCourses(teacherId);
            res.json({ data: courses });
        } catch (error) {
            res.status(500).json({ message: '服务器错误' });
        }
    }
}
